//Documentation complete
/**
 * @author Alex Peterson
 * @version 2009MR20
 */

package ui.internal.ajax;

import control.*;
import problemDomain.*;
import utilities.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Handles all requests for menu item maintenance functions.
 */
public class MaintainEmployeeAccount extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 3039405396803257381L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		response.setContentType("text/xml");
		if (session != null) {
				String action = request.getParameter("a");
				if (action != null) {
					if (action.equalsIgnoreCase("c"))
						create(request, response);
					else if (action.equalsIgnoreCase("r"))
						report(request, response);
					else if (action.equalsIgnoreCase("u"))
						update(request, response);
					else if (action.equalsIgnoreCase("d"))
						delete(request, response);
					else if (action.equalsIgnoreCase("l"))
						listAll(response);
				} else
					response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String position = request.getParameter("position");
		String passcode = request.getParameter("passcode");
		
		PrintWriter out = response.getWriter();
		try {
			EmployeeAccountControl eac = new EmployeeAccountControl();
			eac.create(username, password, firstname, lastname, position, passcode);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String position = request.getParameter("position");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String username = request.getParameter("username");
		


		PrintWriter out = response.getWriter();
		try {
			EmployeeAccountControl eac = new EmployeeAccountControl();
			List<EmployeeAccountBean> reportList = eac.search(firstname, lastname, username,position);
			out.write(XMLTools.encapsulateBeanListXML(reportList, "employees"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String position = request.getParameter("position");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String passCode = request.getParameter("passcode");
		
		PrintWriter out = response.getWriter();
		try {
			EmployeeAccountControl eac = new EmployeeAccountControl();
			eac.update(id, position, firstname, lastname, username, password, passCode);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void delete (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			EmployeeAccountControl eac = new EmployeeAccountControl();
			eac.delete(id);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listAll (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			EmployeeAccountControl eac = new EmployeeAccountControl();
			List<EmployeeAccountBean> list = eac.listAll();
			out.write(XMLTools.encapsulateBeanListXML(list, "employees"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */
	
}
